//
//  MKLoginViewController.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/13.
//  Copyright © 2017年 cc.moko. All rights reserved.
//

import UIKit

class MKLoginViewController: MKBaseViewController {
    var back:UIImageView!
    var loginBtn:UIButton!
    var account:MKLoginInputView!
    var password:MKLoginInputView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.makeUI()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(textFiledChange(notification:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }
    func makeUI() -> Void {
        self.back = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight))
        self.back.image = UIImage.init(named: "MK_RegisterBackGround")
        self.view.addSubview(self.back)
        
        self.titleBar =
            MKTitleBar().titleBar(title: "登录", leftStyle: .None, rightStyle: .None, barStyle: .Default)
        self.view.addSubview(self.titleBar!)
        
        self.account = MKLoginInputView.init(frame: CGRect.init(x: 0, y: 64 + 8, width: ScreenWidth, height: 59))
        self.account.config(type: false)
        self.view.addSubview(self.account)
        
        self.password = MKLoginInputView.init(frame: CGRect.init(x: 0, y: self.account.bottom, width: ScreenWidth, height: 59))
        self.password.config(type: true)
        self.view.addSubview(self.password)
        
        self.loginBtn = UIButton.init(frame:CGRect.init(x: 20, y: self.password.bottom + 24, width: ScreenWidth - 40, height: 49))
        self.loginBtn.setTitleColor(UIColor.white, for: .normal)
        self.loginBtn.backgroundColor = UIColorFromRGB(rgbValue: 0xd3d3d3, A: 1)
        self.loginBtn.layer.cornerRadius = 2
        self.loginBtn.clipsToBounds = true
        self.loginBtn.layer.borderWidth = 0.5
        self.loginBtn.layer.borderColor = UIColorFromRGB(rgbValue: 0xbdbdbd, A: 1).cgColor
        self.loginBtn.titleLabel?.font = fonts(size: 16)
        self.loginBtn.setTitle("登录", for: .normal)
        self.loginBtn.addTarget(self, action: #selector(MKLoginViewController.loginClick), for: .touchUpInside)
        self.view.addSubview(self.loginBtn)
        
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    func loginClick() -> Void {
        print("loginClick")
        
        let moble = self.account.textFiled.text!
        let passWord = self.password.textFiled.text!
        let dic = ["mobile":moble,"passwd":passWord]
        
        MKNetWorkTool.postRequest(url: "/auth", params: dic, success: { (response, responseObject) in
            let model = MKUserInfoResponse.deserialize(from: responseObject)
            if (model?.resultcode?.isEqual("ok"))!{
                
               if let userInfoModel = model?.info{
                    if(userInfoModel.usertype == MKUserType.Businessmen){
                        MKUser.shareUser.saveUserid(userid: userInfoModel.userid!)
                        MKUser.shareUser.saveUserType(userType: userInfoModel.usertype!.rawValue)
                        MKLunchManager.shared.lunch(userType: userInfoModel.usertype!)
                    }
                }
//                 print(userInfoModel?.area!)
//                 print(userInfoModel?.userid!)
//                 print(userInfoModel.leavel!)
//                 print(userInfoModel.usertype!)
            
            }else{
                // 成功的其他情况的展示
            }
        }) { (response, error) in
            
        }
    }
    
    func textFiledChange(notification:NSNotification) -> Void {
        if((self.account.textFiled.text?.characters.count)! > 0 && (self.password.textFiled.text?.characters.count)! > 0){
            self.loginBtn.backgroundColor = UIColorFromRGB(rgbValue: 0xff0051, A: 1)
            self.loginBtn.layer.borderWidth = 0
            self.loginBtn.isEnabled = true
        }else{
            self.loginBtn.backgroundColor = UIColorFromRGB(rgbValue: 0xd3d3d3, A: 1)
            self.loginBtn.layer.borderWidth = 0.5
            self.loginBtn.layer.borderColor = UIColorFromRGB(rgbValue: 0xbdbdbd, A: 1).cgColor
            self.loginBtn.isEnabled = false
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
